package edu.kpi.pzks2.ui.simulation;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import edu.kpi.pzks2.queue.QueueBuilder1;
import edu.kpi.pzks2.queue.QueueBuilder2;
import edu.kpi.pzks2.queue.QueueBuilder3;
import edu.kpi.pzks2.simulator.core.QueueBuilder;

@SuppressWarnings("serial")
public class SimulationPrefsPanel extends JPanel {
    private static final String SIMPLE = "SIMPLE";
    private static final String NEIGHBOUR = "NEIGHBOUR";
    private JTextField processorPerformanceField;
    private JTextField linkSpeedField;
    private JTextField chanelCountField;
    private JCheckBox chckbxDuplex;
    private ButtonGroup queueRadioGroup;
    private ButtonGroup planningRadioGroup;
    private JLabel lblPackageMode;
    private JCheckBox chckbxDivideMessagesInto;
    private JLabel lblStrategy;
    private JRadioButton rdbtnSimple;
    private JRadioButton rdbtnNeighbour;
    
    public SimulationPrefsPanel() {
        setLayout(new MigLayout("", "[grow]", "[][][][][][][][][][][][][][][][][][]"));
        
        add(new JLabel("Queue discipline"), "cell 0 1");
        
        JRadioButton disc1Radio = new JRadioButton("Discipline 1");
        disc1Radio.setSelected(true);
        disc1Radio.setActionCommand(QueueBuilder1.NAME);
        add(disc1Radio, "cell 0 2");
        
        JRadioButton disc2Radio = new JRadioButton("Discipline 2");
        disc2Radio.setActionCommand(QueueBuilder2.NAME);
        add(disc2Radio, "cell 0 3");
        
        JRadioButton disc3Radio = new JRadioButton("Discipline 3");
        disc3Radio.setActionCommand(QueueBuilder3.NAME);
        add(disc3Radio, "cell 0 4");
        
        queueRadioGroup = new ButtonGroup();
        queueRadioGroup.add(disc1Radio);
        queueRadioGroup.add(disc2Radio);
        queueRadioGroup.add(disc3Radio);
        
        
        add(new JLabel("Processor performance"), "cell 0 5");
        
        processorPerformanceField = new JTextField();
        processorPerformanceField.setText("1");
        add(processorPerformanceField, "cell 0 6,growx");
        processorPerformanceField.setColumns(10);
        
        add(new JLabel("Link speed"), "cell 0 7");
        
        linkSpeedField = new JTextField();
        linkSpeedField.setText("1");
        add(linkSpeedField, "cell 0 8,growx");
        linkSpeedField.setColumns(10);
        
        add(new JLabel("Channel count"), "cell 0 9");
        
        chanelCountField = new JTextField();
        chanelCountField.setText("2");
        add(chanelCountField, "cell 0 10,growx");
        chanelCountField.setColumns(10);
        
        add(new JLabel("Channel duplex mode"), "cell 0 11");
        
        chckbxDuplex = new JCheckBox("Duplex");
        chckbxDuplex.setSelected(true);
        add(chckbxDuplex, "cell 0 12");
        
        add(new JLabel("Package mode"), "cell 0 13");
        
        chckbxDivideMessagesInto = new JCheckBox("Divide messages into packages");
        add(chckbxDivideMessagesInto, "cell 0 14");
        
        lblStrategy = new JLabel("Strategy");
        add(lblStrategy, "cell 0 15");
        
        rdbtnSimple = new JRadioButton("Simple");
        rdbtnSimple.setSelected(true);
        rdbtnSimple.setActionCommand(SIMPLE);
        add(rdbtnSimple, "cell 0 16");
        
        rdbtnNeighbour = new JRadioButton("Neighbour");
        rdbtnNeighbour.setActionCommand(NEIGHBOUR);
        add(rdbtnNeighbour, "cell 0 17");
        
        planningRadioGroup = new ButtonGroup();
        planningRadioGroup.add(rdbtnSimple);
        planningRadioGroup.add(rdbtnNeighbour);
    }
    
    public int getLinkSpeed() {
        return Integer.valueOf(linkSpeedField.getText());
    }
    
    public int getProductivity() {
        return Integer.valueOf(processorPerformanceField.getText());
    }
    
    public boolean isDuplex() {
        return chckbxDuplex.isSelected();
    }
    
    public int getChannelCount() {
        return Integer.valueOf(chanelCountField.getText());
    }
    
    public QueueBuilder getQueueBuilder() {
        final String command = queueRadioGroup.getSelection().getActionCommand();
        if (command.equals(QueueBuilder1.NAME)) { return new QueueBuilder1(); }
        if (command.equals(QueueBuilder2.NAME)) { return new QueueBuilder2(); }
        if (command.equals(QueueBuilder3.NAME)) { return new QueueBuilder3(); }
        return null;
    }
    
    public boolean isSimplePlanning() {
        final String command = planningRadioGroup.getSelection().getActionCommand();
        return SIMPLE.equals(command);
    }
    
    public boolean isPackageModeOn() {
        return chckbxDivideMessagesInto.isSelected();
    }
}
